package cn.senjoeson.triper.activity;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bm.library.PhotoView;

import java.util.ArrayList;
import java.util.Arrays;

import butterknife.BindView;
import cn.senjoeson.mylibrary.utilschanged.GlideUtils;
import cn.senjoeson.triper.R;
import cn.senjoeson.triper.base.BaseActivity;

public class PhotoViewActivity extends BaseActivity {

    @BindView(R.id.viewPager)
    ViewPager mViewPager;
    private ArrayList<String> imagesList = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_photo_view;
    }

    @Override
    protected void initData() {
        String imageUrls = getIntent().getStringExtra("imageUrls");

        if (imageUrls != null) {
            if (imageUrls.contains(",")) {
                String[] split = imageUrls.split(",");
                imagesList.addAll(Arrays.asList(split));
            } else {
                imagesList.add(imageUrls);
            }
        }

        mViewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return imagesList == null ? 0 : imagesList.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                // ImageView imageView = new ImageView(container.getContext());
                PhotoView photoView = new PhotoView(container.getContext());
                photoView.enable();
                photoView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                photoView.setMaxScale(2);

                //   String imageUrl = "http://www.senjoeson.cn/img/backgrounds/timg.jpg";
                GlideUtils.loadImage(container.getContext(), imagesList.get(position), photoView);

                photoView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
                container.addView(photoView);
                return photoView;
            }
        });
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initView() {

    }

    @Override
    public void onBackPressed() {
        finish();
    }
}
